# Maintainer: Diego Sogari <diego.sogari@falker.com.br>

_realname=uriparser
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.6
pkgrel=1
pkgdesc="RFC 3986 URI parsing and processing libary (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://uriparser.github.io/"
license=('New BSD License')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gtest>=1.8.1"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/${_realname}/${_realname}/releases/download/${_realname}-${pkgver}/${_realname}-${pkgver}.tar.xz"
        "0001-uriparse-limit-inet_ntop-declaration-to-before-Vista.patch")
sha256sums=('a288a06668528c19e85e38c508335938e1de6fdd4b8f2072401b4533fcebf644'
            'f4a3c015983c5844de245ae8e19c2604bed277c411737e9a9e14dc96cc603039')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-uriparse-limit-inet_ntop-declaration-to-before-Vista.patch"
}

build() {
  # Shared
  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_LIBDIR=lib \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=ON \
      -DURIPARSER_BUILD_DOCS=OFF \
      ../${_realname}-${pkgver}

  cmake --build .

  # Static
  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_LIBDIR=lib \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=OFF \
      -DURIPARSER_BUILD_DOCS=OFF \
      ../${_realname}-${pkgver}

  cmake --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}-shared"
  ninja check || true
}

package() {
  # Static
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" cmake --install .

  # Shared
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" cmake --install .

}
